package test.wrc.com.app;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import test.wrc.com.adapter.FruitAdapter2;
import test.wrc.com.bean.Fruit;

public class RecyclerViewActivity extends BaseActivity {

    private List<Fruit> fruitList = new ArrayList<Fruit>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        initFruits(); // 初始化水果数据
        RecyclerView recycleView = (RecyclerView) findViewById(R.id.recycler_view);

        //=========================
        //ListView
        //        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //=========================
        //横向遍历
        //        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //=========================
        //瀑布流
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        //=========================
        recycleView.setLayoutManager(layoutManager);
        FruitAdapter2 adapter2 = new FruitAdapter2(fruitList);
        recycleView.setAdapter(adapter2);
    }

    private void initFruits() {
        for (int i = 0; i < 100; i++) {
            Fruit apple = new Fruit(getRandomName("Name" + (100 - i)), R.mipmap.ic_launcher);
            fruitList.add(apple);
        }
    }

    private String getRandomName(String name) {
        Random random = new Random();
        int length = random.nextInt(10) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();
    }


}
